package com.yanqu.road.dao.impl.activity.dafuweng;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dafuweng.DFWUnionRecord;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossDFWUnionRecordDaoImpl extends TempDataObjectDao<DFWUnionRecord> {

    public static String getDFWUnionRecordSql = "select * from t_c_dfw_union_record where activityId=? and groupId=?";

    /**
     * Map<类型，Map<商会UID，记录>    类型：1造谣，2祝福
     */
    public Map<Integer, Map<String, List<DFWUnionRecord>>> getDFWUnionRecordMap(int activityId, int groupId) {
        Map<Integer, Map<String, List<DFWUnionRecord>>> resultMap = new ConcurrentHashMap<>();
        Map<String, List<DFWUnionRecord>> zaoYaoMap = new ConcurrentHashMap<>();
        Map<String, List<DFWUnionRecord>> zhuFuMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getDFWUnionRecordSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                DFWUnionRecord record = parse(rs);
                if (record.getRecordType() == 1) {
                    if (!zaoYaoMap.containsKey(record.getUnionUid())) {
                        zaoYaoMap.put(record.getUnionUid(), new ArrayList<>());
                    }
                    zaoYaoMap.get(record.getUnionUid()).add(record);
                } else if (record.getRecordType() == 2) {
                    if (!zhuFuMap.containsKey(record.getUnionUid())) {
                        zhuFuMap.put(record.getUnionUid(), new ArrayList<>());
                    }
                    zhuFuMap.get(record.getUnionUid()).add(record);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        resultMap.put(1, zaoYaoMap);
        resultMap.put(2, zhuFuMap);
        return resultMap;
    }

    public static String updateDFWUnionRecordSql = "update t_c_dfw_union_record set beatBackTimes=? where activityId=? and groupId=? and recordId=?";

    @Override
    protected boolean updateData(DFWUnionRecord dFWUnionRecord) {
        dFWUnionRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionRecord.getBeatBackTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionRecord.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionRecord.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionRecord.getRecordId()));
        if (getDb().executeNoneQuery(updateDFWUnionRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDFWUnionRecordSql = "insert into t_c_dfw_union_record(activityId,groupId,userId,userBaseInfo,recordId,createTime,serverId,unionUid,beatBackTimes,recordType) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DFWUnionRecord dFWUnionRecord) {
        dFWUnionRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionRecord.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionRecord.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWUnionRecord.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUnionRecord.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionRecord.getRecordId()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWUnionRecord.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWUnionRecord.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, dFWUnionRecord.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionRecord.getBeatBackTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionRecord.getRecordType()));
        if (getDb().executeNoneQuery(insertDFWUnionRecordSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DFWUnionRecord parseData(ResultSet rs) throws SQLException {
        DFWUnionRecord dFWUnionRecord = new DFWUnionRecord();
        dFWUnionRecord.setActivityId(rs.getInt("activityId"));
        dFWUnionRecord.setGroupId(rs.getInt("groupId"));
        dFWUnionRecord.setUserId(rs.getLong("userId"));
        dFWUnionRecord.setUserBaseInfo(JSONObject.parseObject(rs.getString("userBaseInfo"), new TypeReference<UserBaseInfo>() {
        }));
        dFWUnionRecord.setRecordId(rs.getInt("recordId"));
        dFWUnionRecord.setCreateTime(rs.getLong("createTime"));
        dFWUnionRecord.setServerId(rs.getLong("serverId"));
        dFWUnionRecord.setUnionUid(rs.getString("unionUid"));
        dFWUnionRecord.setBeatBackTimes(rs.getInt("beatBackTimes"));
        dFWUnionRecord.setRecordType(rs.getInt("recordType"));
        return dFWUnionRecord;
    }
}
